package io.bio;

import io.util.Constants;
import io.util.IOUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 *@Description:<p>客户端IO模型</p>
 *@author 王旭
 *@time 2016年3月17日 上午10:29:09
 */
public class TimeClient {
	
	private Socket socket;
	
	public TimeClient(String host, int port) throws UnknownHostException, IOException {
		socket = new Socket(host, port);
		System.out.println("已连接到服务器");
	}
	
	public void doClient() {
		InputStream in = null;
		OutputStream out = null;
		BufferedReader br = null;
		PrintWriter pw = null;
		
		try {
			in = socket.getInputStream();
			out = socket.getOutputStream();
			pw = new PrintWriter(out, true);
			br = new BufferedReader(new InputStreamReader(in));
			
			//发送请求
			pw.println(Constants.TIME_QUERY);
			//接收回应
			String resp = br.readLine();
			System.out.println("返回信息：" + resp);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtil.close(br, in, pw, out);
			IOUtil.close(socket);
		}
		
	}

}
